﻿using System;
using System.Collections.Generic;
using System.Net;

namespace MiddleServices
{
    public class LogManager
    {
        public static Object objTxtLock = new Object();

        public static string strLocalServerIP = string.Empty;

        public static string strLogFile = string.Empty;

        static LogManager()
        {
            IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());

            foreach (IPAddress ipAddr in ipHost.AddressList)
            {
                //IPAddress ipAddr = ipHost.AddressList[0];
                if (ipAddr.ToString().Split('.').Length == 4)
                {
                    strLocalServerIP += ipAddr.ToString() + ";";
                }
            }

            strLogFile = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\SysWork_" + System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

            //Library.Log.LogManager.Start();
        }

        public static void Write2TXT(string NextLine)
        {

            if (string.IsNullOrEmpty(NextLine.Trim()))
            {
                return;
            }

            lock (objTxtLock)
            {
                //if (new System.IO.FileInfo(strLogFile+".txt").Length > 30 * 1.24 * 1024)
                //{
                //    int n=2;

                //    while (System.IO.File.Exists(strLogFile + n.ToString()+".txt"))
                //    {

                //        n++;
                //    }

                //    strLogFile
                //}

                if (!System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Logs"))
                {
                    System.IO.Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\Logs");
                }

                try
                {
                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(strLogFile, true, System.Text.Encoding.UTF8))
                    {
                        sw.WriteLine(NextLine);
                        //#if !Release
                        //                        Console.WriteLine(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + " " + NextLine);
                        //#endif
                        sw.Flush();
                        sw.Close();
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                }
            }
        }
    }
}
